package com.marketingexpert.dao.ibatis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.marketingexpert.dao.NoticeDao;
import com.marketingexpert.domain.Notice;

public class SqlMapNoticeDao  extends SqlMapClientDaoSupport  implements NoticeDao {

	public int deleteNotice(int[] noticeIds) throws DataAccessException {
		try {
			List<Integer> noticeIdsList = new ArrayList<Integer>();
			for(int noticeId:noticeIds) {
				noticeIdsList.add(noticeId);
			}
			return getSqlMapClientTemplate().delete("deleteNotice", noticeIdsList);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	@Override
	public int publishNotice(Notice notice) throws DataAccessException {
		try {
			return (Integer)getSqlMapClientTemplate().insert("publishNewNotice", notice);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	@Override
	public Notice queryNotice(int noticeId) throws DataAccessException {
		try {
			return (Notice)getSqlMapClientTemplate().queryForObject("publishNewNotice", noticeId);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	@Override
	public List<Notice> viewNotice(int cursor, int pageSize)
			throws DataAccessException {
		try {
			Map<String, Integer> params = new HashMap<String, Integer>();
			params.put("cursor", cursor);
			params.put("pageSize", pageSize);
			return getSqlMapClientTemplate().queryForList("viewNotice", params);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	@Override
	public Notice queryNewNotice(String startTime, String endTime)
			throws DataAccessException {
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("startTime", startTime);
			params.put("endTime", endTime);
			return (Notice)getSqlMapClientTemplate().queryForObject("queryNewNotice", params);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	

}
